//
//  CoastOrderRootFirstVC.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/8/29.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "CoastOrderRootFirstVC.h"
#import "CoastOrderModel.h"
#import "coastOrderCell.h"

@interface CoastOrderRootFirstVC ()<UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *progressHUD;
    UIImageView *noDrawImage;
    NSUInteger refreshIndex;
}

@property(nonatomic,strong)NSMutableArray *coastOrderArray;

@property(nonatomic,strong)UITableView *coastView;

@end

@implementation CoastOrderRootFirstVC

static NSString * const coastId =  @"CoastOrderRootFirstVC";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    refreshIndex = 1;
    
    [self buildView];
    [self coastOrderListRequestData:refreshIndex];
}

- (void)buildView
{
    self.coastView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,WIDTH,HEIGHT-116 ) style:UITableViewStyleGrouped];
    [self.view addSubview:_coastView];
    _coastView.delegate = self;
    _coastView.dataSource = self;
    
    _coastView.backgroundColor = BACKGROUND_COLOR;
    _coastView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_coastView registerNib:[UINib nibWithNibName:@"coastOrderCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:coastId];
    
    [self coastOrderDataRefreshing];
}





///刷新
- (void)coastOrderDataRefreshing
{
    //上啦刷新
    _coastView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(oilOrderListRequestMoreData)];
    
    
}


//上啦更多
- (void)oilOrderListRequestMoreData
{
    refreshIndex ++;
    [self coastOrderListRequestData:refreshIndex];
    [_coastView.mj_footer endRefreshing];
}

#pragma mark >>>>>>>>>获取商品列表
- (void)coastOrderListRequestData:(NSInteger)Index
{
    
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    
    NSDictionary *dict = @{@"username":USERNAME,@"page":[NSString stringWithFormat:@"%zd",Index],@"status":@(6)};
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"/api/Order/callRecharge" completeBlock:^(id object) {
        
        NSLog(@"%@",object);
        if (JudgeBackDataCode) {
            
            NSArray *tempArr = [object objectForKey:@"data"];
            for (NSDictionary *tempDict in tempArr) {
                
                CoastOrderModel *coastOrderModel = [[CoastOrderModel alloc] initWithDictionary:tempDict error:nil];
                if (coastOrderModel) {
                    [self.coastOrderArray addObject:coastOrderModel];
                }
                
            }
            
        }else{
            
            //订单没有
            if (refreshIndex == 1) {
                noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noOrder"]];
                [self.view addSubview:noDrawImage];
                
                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/2);
                    make.height.mas_equalTo(HEIGHT/4);
                }];
            }else{
                
                //没有更多数据
                [_coastView.mj_footer endRefreshingWithNoMoreData];
                
            }
            
        }
        [progressHUD hide:YES];
        [_coastView reloadData];
        
    }];
    
}

#pragma mark >>>>>>>>>>申请退款通知
- (void)applicationForDrawbackButtonClick:(UIButton *)sender
{
    
    CoastOrderModel *coastModel = self.coastOrderArray[sender.tag];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"coastPushToDrawbackVC" object:nil userInfo:@{@"oid":coastModel.oid,@"pay_amount":coastModel.pay_amount}];
}

- (void)applicationForPayOrderButtonClick:(UIButton *)sender
{
    CoastOrderModel *coastModel = self.coastOrderArray[sender.tag];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"coastPushToPay" object:nil userInfo:@{@"oid":coastModel.oid,@"money":coastModel.pay_amount}];
}


#pragma mark >>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.coastOrderArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    coastOrderCell*cell=[tableView dequeueReusableCellWithIdentifier:coastId];
    
    if (cell==nil) {
        cell=[[coastOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:coastId];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CoastOrderModel *coastModel = self.coastOrderArray[indexPath.section];
    
    //区分图片
    if ([coastModel.amount isEqualToString:@"30"]) {
        cell.bgImg.image = [UIImage imageNamed:@"coast30.jpg"];
    }else if ([coastModel.amount isEqualToString:@"50"]){
        cell.bgImg.image = [UIImage imageNamed:@"coast50.jpg"];
    }else{
        cell.bgImg.image = [UIImage imageNamed:@"coast100.jpg"];
    }
    
    //订单状态
    cell.statusPayLabel.textColor = [UIColor redColor];
    if ([coastModel.status isEqualToString:@"0"]) {
        cell.statusPayLabel.text = @"未付款";
    }else if ([coastModel.status isEqualToString:@"1"]){
        cell.statusPayLabel.text = @"充值成功";
    }else if ([coastModel.status isEqualToString:@"2"]){
        cell.statusPayLabel.text = @"充值中";
    }else if ([coastModel.status isEqualToString:@"3"]){
        cell.statusPayLabel.text = @"充值失败";
    }else if ([coastModel.status isEqualToString:@"4"]){
        cell.statusPayLabel.text = @"已退款(充值失败)";
    }else{
        cell.statusPayLabel.text = @"";
    }
    
    
    //充值状态
    cell.drawbackBtn.titleLabel.font = FONT(12);
    cell.drawbackBtn.backgroundColor = DEFAULT_COLOR;
    cell.drawbackBtn.layer.cornerRadius = 3;
    cell.drawbackBtn.layer.masksToBounds = YES;
    
    if ([coastModel.status_text isEqualToString:@"未付款"]) {
        cell.drawbackBtn.hidden = false;
        [cell.drawbackBtn setTitle:@"付款" forState:UIControlStateNormal];
        [cell.drawbackBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cell.drawbackBtn addTarget:self action:@selector(applicationForPayOrderButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.drawbackBtn.tag = indexPath.section;
        
    }else if ([coastModel.status isEqualToString:@"3"]){
        cell.drawbackBtn.hidden = false;
        cell.drawbackBtn.backgroundColor = [UIColor whiteColor];
        [cell.drawbackBtn setTitle:@"申请退款" forState:UIControlStateNormal];
        [cell.drawbackBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        cell.drawbackBtn.layer.borderWidth = 0.5;
        cell.drawbackBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [cell.drawbackBtn addTarget:self action:@selector(applicationForDrawbackButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.drawbackBtn.tag = indexPath.section;
        
    }else{
        
        cell.drawbackBtn.hidden = YES;
    }
    
    //订单号
    cell.oidOrderLabel.text = [NSString stringWithFormat:@"订单号: %@",coastModel.oid];
    //手机号
    cell.accountNoLabel.text = [NSString stringWithFormat:@"充值账户: %@",coastModel.account];
    //运营商
    cell.ztLabel.text = [NSString stringWithFormat:@"运营商: %@",coastModel.t_mobile];
    //金额
    cell.nameLabel.text = [NSString stringWithFormat:@"付款金额: %@元",coastModel.pay_amount];
    
    //时间
    cell.timeLabel.text = coastModel.create_time;
    
    //支付方式
    if (![coastModel.status isEqualToString:@"0"]) {
        
        if ([coastModel.pay_type isEqualToString:@"1"]) {
            
            NSMutableAttributedString * mStr = [[NSMutableAttributedString alloc]initWithString:@"支付类型: 充值账户 (已付款)"];
            NSDictionary * dict1 = @{
                                     NSForegroundColorAttributeName:[UIColor redColor]};
            [mStr addAttributes:dict1 range:NSMakeRange(6, 10)];
            cell.rcMoneyLabel.attributedText = mStr;
            
        }else if ([coastModel.pay_type isEqualToString:@"2"]){
            
            NSMutableAttributedString * mStr = [[NSMutableAttributedString alloc]initWithString:@"支付类型: 代金券 (已付款)"];
            NSDictionary * dict1 = @{
                                     NSForegroundColorAttributeName:[UIColor redColor]};
            [mStr addAttributes:dict1 range:NSMakeRange(6, 9)];
            cell.rcMoneyLabel.attributedText = mStr;
            
        }else if ([coastModel.pay_type isEqualToString:@"3"]){
            
            NSMutableAttributedString * mStr = [[NSMutableAttributedString alloc]initWithString:@"支付类型: 支付宝 (已付款)"];
            NSDictionary * dict1 = @{
                                     NSForegroundColorAttributeName:[UIColor redColor]};
            [mStr addAttributes:dict1 range:NSMakeRange(6, 9)];
            cell.rcMoneyLabel.attributedText = mStr;
            
        }else if ([coastModel.pay_type isEqualToString:@"4"]){
            
            NSMutableAttributedString * mStr = [[NSMutableAttributedString alloc]initWithString:@"支付类型: 微信 (已付款)"];
            NSDictionary * dict1 = @{
                                     NSForegroundColorAttributeName:[UIColor redColor]};
            [mStr addAttributes:dict1 range:NSMakeRange(6, 8)];
            cell.rcMoneyLabel.attributedText = mStr;
            
        }else{
            
            cell.rcMoneyLabel.text = @"支付类型: 未付款";
        }
        
    }else{
        
        cell.rcMoneyLabel.text = @"支付类型: 未付款";
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 165;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (NSMutableArray *)coastOrderArray
{
    if (!_coastOrderArray) {
        _coastOrderArray = [NSMutableArray array];
    }
    return _coastOrderArray;
}



@end
